*==============================================================================
* Prepare datasets for regression
cap program drop est_ready_pf
program define est_ready_pf
args y1 y2
	
	use "$Data/est_ready_by_prefecture.dta", clear
	
	* prepare log prices
	qui gen avgp_prior=ptilde if year == `y1'
	qui gen avgp_post=ptilde if year == `y2' 
	
	qui gen avglp1 = log(avgp_prior)
	qui gen avglp2 = log(avgp_post)
	
	qui label var avglp1 "quality adjusted log price in `y1'"
	qui label var avglp2 "quality adjusted log price in `y2'"

	* save variable labels
	foreach v of var avglp1 avglp2 te TN_DW x_NS* xcat_NS* xdd_NS* xdds_NS* prefecture_id RPS{
		local l`v' : variable label `v'
        if `"`l`v''"' == "" {
 		local l`v' "`v'"
		}
	}
	
	* collapse to have unique values by RPS and prefecture_id
	collapse (mean) avglp1 avglp2 te TN_DW x_NS* xcat_NS* xdd_NS* xdds_NS*, by(RPS prefecture_id)
	
	*label variabless
	foreach v of var * {
		label var `v' "`l`v''"
	}
  
	gen year1 = `y1'
	gen year2 = `y2'
	local x = `y2'-`y1'

	
	* generate price change
	qui gen delta_avgp = avglp2 - avglp1
	drop if missing(delta_avgp)

	* discard incomplete observations
	qui keep if !missing(avglp1) & !missing(avglp2)
			
	* e-commerce intensity times price in the previous period
	*qui gen xlp1_rakuten = x_rakuten*avglp1 /* Rakuten*/
	*label var xlp1_rakuten "\$ x(Rakuten) \times \Delta p_{ipt-`x'}$"
	foreach year in 2014 {
		qui gen xlp1_NS`year' = x_NS`year'*avglp1 
		label var xlp1_NS`year' "\$ x(NS`year') \times \Delta p_{ipt-`x'}$"
	}
	
	* category intensity times price in the previous period
	foreach year in 1999 2014 {
		qui gen xcatlp1_NS`year' = xcat_NS`year'*avglp1 
		label var xcatlp1_NS`year' "\$ xcat(NS`year') \times \Delta p_{ipt-`x'}$"
	}
	
	* merge METI numbers
	merge m:1 year2 using "$Data/METI_numbers", keep(1 3) nogen
	
	* label variables
	label var delta_avgp  "\$\Delta p_{ipt}$"
	label var avglp1	"\$ p_{ic`y1'} $"
	local var te 		"\$\tilde{e}$"
	
	* save datasets

	cd "$Data/pricereg"
	save "`y1'_`y2'_reg_by_prefecture.dta", replace

end 

*==============================================================================
* One period regression specification
cap program drop pricereg_pf
program define pricereg_pf
args y1 y2 def goods iv dummy

	local fixed_effect = "prefdum_t product_t"
		
	use "$Data/pricereg\\`y1'_`y2'_reg_by_prefecture.dta", clear
	drop if TN_DW == 1 & "`goods'" == "tr"

	egen prefdum_t = group(prefecture_id year2)
	egen product_t = group(RPS year2)
	
	egen pref_gr=group(prefecture_id)
	egen product_gr = group(RPS)
	
	* limit to products and cities that exist in all relevant years
	foreach year in `y2' {
		cap qui gen b_pref_`year' = 1 if (year2 == `year')& avglp1 ~= .
		sort prefecture_id b_pref_`year'
		cap qui by prefecture_id: replace b_pref_`year' = b_pref_`year'[_n-1] if missing(b_pref_`year') & _n~= 1
		
		cap qui gen b_product_`year' = 1 if (year2 == `year') & avglp1 ~= .
		sort RPS b_product_`year'
		cap qui by RPS: replace b_product_`year' = b_product_`year'[_n-1] if missing(b_product_`year') & _n~= 1
	}
	
	ds b_product* b_pref*
	foreach var in `r(varlist)'{
		qui keep if `var' == 1
	}
	
	* generate dummy for period 2 interacte with xp
	if "`dummy'"=="d_1997"{
		qui gen dummy = 1 if year2>= 1997
		qui replace dummy = 0 if dummy == .
	}
	if "`dummy'"=="METI_size"{
		qui gen dummy = METI_log_size
		qui replace dummy = 0 if dummy == .
	}
	if "`dummy'"=="METI_share"{
		qui gen dummy = METI_share
		qui replace dummy = 0 if dummy == .
	}

	
	if "`def'" ==  "x_rakuten" {
		gen xlp=xlp1_rakuten
		gen d_xlp1 = xlp*dummy
		local x_year "2010"
	}
	
	if "`def'" == "x_NS2014" {
		gen xlp=xlp1_NS2014
		gen d_xlp1 = xlp*dummy
		local x_year "2014"
	}
	
	gen xcatlp = xcatlp1_NS1999
	gen d_xcatlp1 = dummy*xcatlp1_NS1999
	gen d_p = dummy*avglp1	

	if `y2' >= 1997 & "`iv'" == "ols"{ 
		eststo: reghdfe delta_avgp avglp1 d_xlp1, absorb(`fixed_effect', save) vce(cluster prefecture_id RPS)
		estadd local iv "OLS"
		estadd scalar r_squared `e(r2)'
	}
	if `y2' < 1997 &  "`iv'" == "ols"{ 
		eststo: reghdfe delta_avgp avglp1 xlp, absorb(`fixed_effect', save) vce(cluster prefecture_id RPS)
		estadd local iv "OLS"
		estadd scalar r_squared `e(r2)'
	}
	if `y2' >= 1997 &  "`iv'" == "2sls"{ 			
		eststo: ivreghdfe delta_avgp avglp1 (d_xlp1=d_xcatlp1), absorb(`fixed_effect') cluster(prefecture_id RPS) savefirst first saverf 
		estadd scalar fs = e(widstat)
		estadd local iv "IV"
		estadd local pr "\{`y2'\}"
		estadd local "x_t" `x_year'
		estadd local k `=`y2'-`y1''		
	}	
	if `y2' < 1997 &  "`iv'" == "2sls"{ 		
		eststo: ivreghdfe  delta_avgp avglp1 (xlp=xcatlp), absorb(`fixed_effect') cluster(prefecture_id RPS) savefirst first saverf
		estadd scalar fs = e(widstat)
		estadd local iv "IV"
		estadd local pr "\{`y2'\}"
		estadd local "x_t" `x_year'
		estadd local k `=`y2'-`y1''		
	}
	

	
end

*==============================================================================
* Diff-in-Diff regression  specification
cap program drop reg_dind_pf
program define reg_dind_pf
args y1 y2 y3 y4 def goods iv dummy

*reg_dind 1991 1996 1996 2001 `def' "`fe'" tr `est' "`dummy'"


	local fixed_effect = "prefdum_t product_t"
	
	cd "$Data/pricereg"
	use "`y1'_`y2'_reg_by_prefecture.dta", clear
	append using "`y3'_`y4'_reg_by_prefecture.dta"

	* generate variable for fixed effects that vary for each year
	egen prefdum_t = group(prefecture_id year2)
	egen product_t = group(RPS year2)
	
	egen pref_gr=group(prefecture_id)
	egen product_gr = group(RPS)
			
	* find the product, prefecture, and product-city pairs that exist in each year
	forvalues i = 2(2)4 {
		cap qui gen b_pref_`y`i'' = 1 if (year2 == `y`i'')& avglp1 ~= .
		sort prefecture_id b_pref_`y`i''
		cap qui by prefecture_id: replace b_pref_`y`i'' = b_pref_`y`i''[_n-1] if missing(b_pref_`y`i'') & _n~= 1
		
		cap qui gen b_product_`y`i'' = 1 if (year2 == `y`i'') & avglp1 ~= .
		sort RPS b_product_`y`i''
		cap qui by RPS: replace b_product_`y`i'' = b_product_`y`i''[_n-1] if missing(b_product_`y`i'') & _n~= 1
	}	
	
	* keep if product exists in all 4 years, keep if city exist in all 4 years
	ds b_product* b_pref*
	foreach var in `r(varlist)'{
		qui keep if `var' == 1
	}
	
	* generate D*xlp, where D is a dummy that's 1 if in the second period
	if "`dummy'"=="d_1997"{
		qui gen dummy = 1 if year2>= 1997
		qui replace dummy = 0 if dummy == .
	}
	if "`dummy'"=="METI_size"{
		qui gen dummy = METI_log_size
		qui replace dummy = 0 if dummy == .
	}
	if "`dummy'"=="METI_share"{
		qui gen dummy = METI_share
		qui replace dummy = 0 if dummy == .
	}
	
	if "`def'" == "x_NS2014" {
		gen xlp=xlp1_NS2014
		gen d_xlp1 = xlp*dummy
		local x_year "2014"
	}
	
	gen xcatlp = xcatlp1_NS1999
	gen d_xcatlp1 = dummy*xcatlp1_NS1999
	gen d_p = dummy*avglp1
	* runs difference in difference specification
	if "`iv'" == "ols" {
		eststo: reghdfe delta_avgp avglp1 xlp d_xlp1 d_p, absorb(`fixed_effect', save) vce(cluster prefecture_id RPS)
		estadd scalar r_squared `e(r2)'
	}
	if "`iv'" == "2sls" {
		eststo: ivreghdfe delta_avgp avglp1 d_p (xlp d_xlp1 = xcatlp d_xcatlp1), absorb(`fixed_effect') cluster(prefecture_id  RPS)  savefirst first saverf savefprefix(col3_st1)
		estadd scalar fs = e(widstat)
		estadd local iv "IV"
		estadd local pr "\{`y2',`y4'\}": col3_st1xlp
		estadd local pr "\{`y2',`y4'\}": col3_st1d_xlp1 
		* add statistics to output
		estadd local pr "\{`y2',`y4'\}"
		estadd local "x_t" `x_year'
		estadd local k `=`y2'-`y1''	
	}
	
	
end

*=================================================================================
* Diff-in-Diff annual regression specification
cap program drop annual_reg_pf
program define annual_reg_pf
args yr2_st yr2_end def goods iv dummy

	local fixed_effect = "prefdum_t product_t"
	
	qui use "$Data/pricereg//`=`yr2_st'-1'_`yr2_st'_reg_by_prefecture.dta", clear
	forvalues y1 = `=`yr2_st''/`=`yr2_end'-1' {
		qui append using "$Data/pricereg//`y1'_`=`y1'+1'_reg_by_prefecture.dta"
	}
	
	qui drop if TN_DW == 1 & "`goods'" == "tr"

	egen prefdum_t = group(prefecture_id year2)
	egen product_t = group(RPS year2)
	
	egen pref_gr=group(prefecture_id)
	egen product_gr = group(RPS)
			
	* limit to products and cities that exist in all relevant years
	forvalues year = `yr2_st'/`yr2_end' {
		cap qui gen b_pref_`year' = 1 if (year2 == `year')& avglp1 ~= .
		sort prefecture_id b_pref_`year'
		cap qui by prefecture_id: replace b_pref_`year' = b_pref_`year'[_n-1] if missing(b_pref_`year') & _n~= 1
		
		cap qui gen b_product_`year' = 1 if (year2 == `year') & avglp1 ~= .
		sort RPS b_product_`year'
		cap qui by RPS: replace b_product_`year' = b_product_`year'[_n-1] if missing(b_product_`year') & _n~= 1
	}

	ds b_product* b_pref*
	foreach var in `r(varlist)'{
		qui keep if `var' == 1
	}

	* generate dummy for period 2 interacte with xp
	if "`dummy'"=="d_1997"{
		qui gen dummy = 1 if year2>= 1997
		qui replace dummy = 0 if dummy == .
	}
	if "`dummy'"=="METI_size"{
		qui gen dummy = METI_log_size
		qui replace dummy = 0 if dummy == .
	}
	if "`dummy'"=="METI_share"{
		qui gen dummy = METI_share
		qui replace dummy = 0 if dummy == .
	}
	
	if "`def'" == "x_NS2014" {
		qui gen xlp=xlp1_NS2014
		qui gen d_xlp1 = xlp*dummy
		local x_year "2014"
	}
	
	qui gen xcatlp = xcatlp1_NS1999
	qui gen d_xcatlp1 = dummy*xcatlp1_NS1999
	qui gen d_p = dummy*avglp1
	
	* runs difference in difference specification
	if "`iv'" == "ols" {
		eststo: reghdfe delta_avgp avglp1 xlp d_xlp1 d_p, absorb(`fixed_effect', save) vce(cluster prefecture_id RPS) resid
		estadd local pr "Annual"
		estadd local "x_t" `x_year'
		estadd scalar r_squared `e(r2)'
	}
	if "`iv'" == "2sls" {
	
		eststo: ivreghdfe delta_avgp avglp1 d_p (xlp d_xlp1 = xcatlp d_xcatlp1 ), absorb(`fixed_effect', save resid(residuals1)) cluster(prefecture_id RPS) savefirst first saverf
		mat fstat = e(first)
		estadd local iv "IV"		
		estadd local pr "Annual"
		estadd local pr2 "`yr2_st'-`yr2_end'"
		estadd local "x_t" `x_year'
		estadd scalar fs = e(widstat)
	
	}
		
	
		
end

*=================================================================================
* DinD specification: main results
cap program drop reg_dind_base_pf
program define reg_dind_base_pf
args def dummy no
	
	local fixed_effect = "prefdum_t product_t"
	
	if "`dummy'"=="d_1997" local suff2 ""
	if "`dummy'"=="METI_size" local suff2 "Msize_"	
	
	foreach est in 2sls{
		
		est clear
		
		pricereg_pf 1992 1996 `def' tr `est' "`dummy'" /*column1 of table 5*/
		pricereg_pf 1996 2000 `def' tr `est' "`dummy'" /*column2 of table 5*/
		reg_dind_pf 1992 1996 1996 2000 `def' tr `est' "`dummy'" /*column3 of table 5*/
		annual_reg_pf 1992 2000 `def' tr `est' "`dummy'" /*column4 of table 5*/
		
		label var delta_avgp  "\$\Delta$ \$ p_{ipt}$"
		label var avglp1 "Lagged Price "
		label var xlp "E-Commerce Intensity(p) \$\times$ Lagged Price "
		
		if "`dummy'"=="d_1997" {
			* label variables
			label var d_xlp1 "\$ D_{t}$ \$\times$ E-Commerce Intensity(p) \$\times$ Lagged Price"
			label var d_p "\$ D_{t}$ \$\times$ Lagged Price"
			
		}
		if "`dummy'"=="METI_size" {
			* label variables
			label var d_xlp1 "EC Market Size \$\times$ E-Commerce Intensity(p) \$\times$ Lagged Price"
			label var d_p "EC Market Size \$\times$ Lagged Price "
		}
				
		local suff = ""
		
		local fname "$table/table_`no'.tex"
		local fname_temp "$table/tmp.tex"
		
		if "`est'" == "ols"{
		esttab using "`fname'", order(avglp1 xlp d_p d_xlp1) keep(avglp1 xlp d_p d_xlp1) b(3) se(3) ar2(3) ///
				nolegend nonotes star(* 0.10 ** 0.05 *** 0.01) replace label wrap substitute(\_ _) ///
				stats(pr N r_squared, labels("\$t$" "Observations" "\$R^{2}$") fmt(%50 %9.0fc %9.2fc)) 
		}	
		if "`est'"=="2sls" {
		esttab using "`fname'", order(avglp1 xlp d_p d_xlp1) keep(avglp1 xlp d_p d_xlp1) b(3) se(3) ar2(3) ///
				nolegend nonotes star(* 0.10 ** 0.05 *** 0.01) replace label wrap substitute(\_ _) ///
				stats(pr N fs iv, labels("\$t$" "Observations" "First-stage F" "Estimation") fmt(%50 %9.0fc %9.2fc %50)) 
		}
				
		filefilter "`fname'" "`fname_temp'", ///
			from ("Annual\BS\BS") to ("Annual\BS\BS\n&&&&1991-2001\BS\BS") replace
		filefilter "`fname_temp'" "`fname'", ///
			from ("                    &\BSmulticolumn{1}{c}{$\BSDelta p_{ipt}$}") to ("Dependent Variable&\BSmulticolumn{1}{c}{$\BSDelta p_{ipt}$}") replace
		filefilter "`fname'" "`fname_temp'", ///
			from ("\BSbegin{tabular}") to ("\BSadjustbox{max width=\BStextwidth}{\n\BSbegin{tabular}") replace
		filefilter "`fname_temp'" "`fname'", ///
			from ("\BSend{tabular}") to ("\BSend{tabular}\n}") replace	
		filefilter "`fname'" "`fname_temp'", ///
			from ("EC Market Size") to ("Log E-Commerce Market Size") replace		
		filefilter "`fname_temp'" "`fname'", replace
			
		rm "`fname_temp'"
		
	}
end


*=================================================================================================
* Relative Price Change
cap program drop relative_price_change_pf
program define relative_price_change_pf
args def dummy no

	est clear
	if "`dummy'"=="d_1997" local suff2 ""
	if "`dummy'"=="METI_size" local suff2 "_Msize"
	
	
	* 1992-2001 tradable goods sample
	use "$Data/pricereg/1991_1992_reg_by_prefecture.dta", clear
	forvalues y1 = 1992/2000 {
		qui append using "$Data/pricereg//`y1'_`=`y1'+1'_reg_by_prefecture.dta"
	}
	
	drop if TN_DW == 1

	* limit to products and cities that exist in all relevant years
	forvalues year = 1992/2001 {
		cap qui gen b_pref_`year' = 1 if (year2 == `year')& avglp1 ~= .
		sort prefecture_id b_pref_`year'
		cap qui by prefecture_id: replace b_pref_`year' = b_pref_`year'[_n-1] if missing(b_pref_`year') & _n~= 1
		
		cap qui gen b_product_`year' = 1 if (year2 == `year') & avglp1 ~= .
		sort RPS b_product_`year'
		cap qui by RPS: replace b_product_`year' = b_product_`year'[_n-1] if missing(b_product_`year') & _n~= 1
	}

	ds b_product* b_pref*
	foreach var in `r(varlist)'{
		qui keep if `var' == 1
	}
	drop b_product* b_pref*

	* generate dummy and fixed effect groups
	if "`dummy'"=="d_1997"{
		qui gen dummy = 1 if year2>= 1997
		qui replace dummy = 0 if dummy == .
	}
	if "`dummy'"=="METI_size"{
		qui gen dummy = METI_log_size
		qui replace dummy = 0 if dummy == .
	}
	if "`dummy'"=="METI_share"{
		qui gen dummy = METI_share
		qui replace dummy = 0 if dummy == .
	}
	egen pref_gr=group(prefecture_id)  
	egen product_gr = group(RPS)
	
	gen d_x = dummy*`def'
	gen d_xcat = dummy*xcat_NS1999
	gen d_p = dummy*avglp1
	
	if "`def'" == "x_rakuten"{
		local year 2010
	}
	else{
		local year `=substr("`def'",-4,4)'
	}
	
	*column 1 1992-2001 ols items fixed effect
	reghdfe delta_avgp dummy d_x, absorb(product_gr) vce(cluster prefecture_id RPS)
	eststo inflation_1_a
	estadd local fe "Product"
	estadd local sample "Goods"
	estadd local x_t "`year'"
	estadd local t "1992-2001"
	estadd local twosls "OLS"
	estadd scalar r_squared `e(r2)'
	
	
	*column 3 1992-2001 2sls items fixed effect
	eststo: ivreghdfe delta_avgp dummy (d_x=d_xcat), absorb(product_gr) cluster(prefecture_id RPS) savefirst first savefprefix(inflation_1_b_st1)
	eststo inflation_1_b
	estadd local fe "Product": inflation_1_b*
	estadd local sample "Goods": inflation_1_b*
	estadd local x_t "`year'": inflation_1_b*
	estadd local t "1992-2001": inflation_1_b*
	local fst = e(widstat)
	estadd scalar fs = `fst': inflation_1_b*
	estadd scalar f1s = `fst': inflation_1_b_st1*
	estadd local twosls "IV"
	estadd local twosls "IV-First Stage": inflation_1_b_st1*
	mat fstat = e(first)
	local pr2 fstat[2,1]
	local r_squared : display %9.2f `pr2'
	estadd local r_squared `r_squared': inflation_1_b_st1*

	* 1992-2016 tradable goods sample
	use "$Data/pricereg/1991_1992_reg_by_prefecture.dta", clear
	forvalues y1 =  1992/2015  {
		qui append using "$Data/pricereg//`y1'_`=`y1'+1'_reg_by_prefecture.dta"
	}
	
	drop if TN_DW == 1

	* limit to products and cities that exist in all relevant years
	forvalues year = 1992/2016 {
		cap qui gen b_pref_`year' = 1 if (year2 == `year')& avglp1 ~= .
		sort prefecture_id b_pref_`year'
		cap qui by prefecture_id: replace b_pref_`year' = b_pref_`year'[_n-1] if missing(b_pref_`year') & _n~= 1
		
		cap qui gen b_product_`year' = 1 if (year2 == `year') & avglp1 ~= .
		sort RPS b_product_`year'
		cap qui by RPS: replace b_product_`year' = b_product_`year'[_n-1] if missing(b_product_`year') & _n~= 1
	}

	ds b_product* b_pref*
	foreach var in `r(varlist)'{
		qui keep if `var' == 1
	}
	drop b_product* b_pref*

	* generate dummy and fixed effect groups
	if "`dummy'"=="d_1997"{
		qui gen dummy = 1 if year2>= 1997
		qui replace dummy = 0 if dummy == .
	}
	if "`dummy'"=="METI_size"{
		qui gen dummy = METI_log_size
		qui replace dummy = 0 if dummy == .
	}
	if "`dummy'"=="METI_share"{
		qui gen dummy = METI_share
		qui replace dummy = 0 if dummy == .
	}
	egen pref_gr=group(prefecture_id)  
	egen product_gr = group(RPS)
	
	gen d_x = dummy*`def'
	gen d_xcat = dummy*xcat_NS1999
	gen d_p = dummy*avglp1
	
	if "`def'" == "x_rakuten"{
		local year 2010
	}
	else{
		local year `=substr("`def'",-4,4)'
	}
	
	*column 2 1992-2016 ols item fixed effect
	reghdfe delta_avgp dummy d_x, absorb(product_gr, save) vce(cluster prefecture_id RPS)
	eststo inflation_2_a
	estadd local fe "Product"
	estadd local sample "Goods"
	estadd local x_t "`year'"
	estadd local t "1992-2016"
	estadd local twosls "OLS"
	estadd scalar r_squared `e(r2)'
	
	*column 4 1992-2016 2sls item fixed effect
	eststo: ivreghdfe delta_avgp dummy (d_x=d_xcat), absorb(product_gr) cluster(prefecture_id  RPS) savefirst first savefprefix(inflation_2_b_st1)
	eststo inflation_2_b
	estadd local fe "Product": inflation_2_b*
	estadd local sample "Goods": inflation_2_b*
	estadd local x_t "`year'": inflation_2_b*
	estadd local t "1992-2016": inflation_2_b*
	*estadd scalar fs = e(widstat): inflation_2_b*
	*estadd scalar f1s = `fs': inflation_2_b_st1*
	local fst = e(widstat)
	estadd scalar fs = `fst': inflation_2_b*
	estadd scalar f1s = `fst': inflation_2_b_st1*
	estadd local twosls "IV"
	estadd local twosls "IV-First Stage": inflation_2_b_st1*
	mat fstat = e(first)
	local pr2 fstat[2,1]
	local r_squared : display %9.2f `pr2'
	estadd local r_squared `r_squared': inflation_2_b_st1*
	
	* Cross section regression
	use "$Data/pricereg/2008_2009_reg_by_prefecture.dta", clear
	keep `def' xcat_NS1999 RPS
	drop if `def' == . | xcat_NS1999 == .
	duplicates drop RPS, force
	reg `def' xcat_NS1999
	eststo inflation_3
	estadd local fe "None"
	estadd local sample "Goods"
	estadd local x_t "`year'"
	estadd local t ""
	estadd local twosls "OLS"
	estadd scalar r_squared `e(r2)'
	
	foreach var in dummy d_x delta_avgp d_xcat d_p {
		qui gen `var' = .
	}
	
	
	if "`dummy'"=="d_1997" {
		
		label var dummy "\$D_{t}$"
		label var d_x "E-Commerce Intensity(p) \$\times$ \$ D_{t}$"
		label var d_p "Lagged Price \$\times$ \$ D_{t}$"
		label var d_xcat "Catalog Intensity \$\times$ \$ D_{t}$"
	}
	
	if "`dummy'"=="METI_size" {
		
		label var dummy "EC Market Size"
		label var d_x "E-Commerce Intensity(p) \$\times$ EC Market Size"
		label var d_p "Lagged Price \$\times$ EC Market Size"
		label var d_xcat "Catalog Intensity \$\times$ EC Market Size"
	}
	
	label var delta_avgp  "\$\Delta$ $ p_{ipt}$"
	label var `def' "E-Commerce Intensity(p)"
	label var xcat_NS1999 "Catalog Intensity"
	
	local fname "$table/table_`no'.tex"
	local fname_temp "$table/tmp.tex"
	
	esttab inflation_1_a inflation_2_a inflation_1_b inflation_2_b ///
		using "`fname'", ///
		keep(dummy d_x) order(dummy d_x) b(4) se(4) ar2(a3) ///
		nolegend nonotes star(* 0.10 ** 0.05 *** 0.01) replace label wrap substitute(\_ _) ///
		stats(fe t N r_squared fs twosls, labels("Fixed Effects" "Estimation Period" "Observations" "\$R^{2}$" "First-Stage F-Stat" "Estimation Method") fmt(%50 %50 %9.0fc %9.2fc %9.2fc %50))
		
	filefilter "`fname'" "`fname_temp'", ///
		from ("\BSbegin{tabular}") to ("\BSadjustbox{max width=\BStextwidth}{\n\BSbegin{tabular}") replace
	filefilter "`fname_temp'" "`fname'", ///
		from ("\BSend{tabular}") to ("\BSend{tabular}\n}") replace
	filefilter "`fname'" "`fname_temp'", ///
		from ("times&") to ("times\$&") replace
	filefilter "`fname_temp'" "`fname'", ///
		from ("\nD_") to ("\n\$D_}") replace
	filefilter "`fname'" "`fname_temp'", ///
		from ("EC Market Size") to ("Log E-Commerce Market Size") replace
	filefilter "`fname_temp'" "`fname'", replace 	
	rm "`fname_temp'"
	
end

